//
//  InputViewController.swift
//  Ofo-demo
//
//  Created by 张彦辉 on 2017/5/24.
//  Copyright © 2017年 张彦辉. All rights reserved.
//

import UIKit
import APNumberPad

class InputViewController: UIViewController {

    @IBOutlet weak var inputText: UITextField!
    @IBOutlet weak var goBtn: UIButton!
    
    @IBAction func goBtnClick(_ sender: UIButton) {
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "车辆解锁"
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "扫码", style: .plain, target: self, action: #selector(backScan))
        inputText.layer.borderWidth = 2
        inputText.layer.borderColor = UIColor.yellowOfo.cgColor
        inputText.delegate = self
        
        //键盘初始化
        let numberPad = APNumberPad(delegate: self)
        numberPad.leftFunctionButton.setTitle("确认", for: .normal)
        inputText.inputView = numberPad
        
    }
    
    func backScan() {
        self.navigationController?.popViewController(animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
extension InputViewController:APNumberPadDelegate{
    //自定义功能按钮回调事件
    func numberPad(_ numberPad: APNumberPad, functionButtonAction functionButton: UIButton, textInput: UIResponder) {
        
    }
}
extension InputViewController:UITextFieldDelegate{
    
    // MARK:
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        guard let text = textField.text else {
            return true
        }
        let lengthStr = text.characters.count + string.characters.count - range.length
        let isTrue = lengthStr <= 8
        if isTrue {
            if lengthStr == 8 {
                goBtn.setImage(#imageLiteral(resourceName: "nextArrow_enable"), for: .normal)
                goBtn.backgroundColor = UIColor.yellowOfo
            } else {
                goBtn.setImage(#imageLiteral(resourceName: "nextArrow_unenable"), for: .normal)
                goBtn.backgroundColor = UIColor.groupTableViewBackground
            }
        }
        return isTrue
    }
}
